package com.ruoyi.web.controller.medical.checks;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.users.domain.Patient;
import com.ruoyi.users.service.IPatientService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.medical.domain.Checks;
import com.ruoyi.medical.service.IChecksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检查结果Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/medical/checks")
public class ChecksController extends BaseController {
    @Autowired
    private IChecksService checksService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询检查结果列表
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:list')")
    @GetMapping("/list")
    public TableDataInfo list(Checks checks) {
        startPage();
        List<Checks> list = checksService.selectChecksList(checks);
        return getDataTable(list);
    }

    /**
     * 导出检查结果列表
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:export')")
    @Log(title = "检查结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Checks checks) {
        List<Checks> list = checksService.selectChecksList(checks);
        ExcelUtil<Checks> util = new ExcelUtil<Checks>(Checks.class);
        util.exportExcel(response, list, "检查结果数据");
    }

    /**
     * 获取检查结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(checksService.selectChecksById(id));
    }

    /**
     * 新增检查结果
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:add')")
    @Log(title = "检查结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Checks checks) {
        SysUser patient = userService.checkPatientAndDoctor(checks.getPatientName(), checks.getIdNumber(), null, null);
        if (patient == null) {
            return AjaxResult.error("患者姓名或身份证号输入错误");
        }
        if (!"11".equals(patient.getUserType())) {
            return AjaxResult.error("该用户不属于患者用户");
        }
        SysUser doctor = userService.checkPatientAndDoctor(null, null, checks.getDoctorName(), checks.getDoctorId());
        if (doctor == null) {
            return AjaxResult.error("医生姓名或工号输入错误");
        }
        if (!"00".equals(doctor.getUserType()) || "admin".equals(doctor.getUserName())) {
            return AjaxResult.error("该用户不属于医生用户");
        }
        return toAjax(checksService.insertChecks(checks));
    }

    /**
     * 修改检查结果
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:edit')")
    @Log(title = "检查结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Checks checks) {
        SysUser patient = userService.checkPatientAndDoctor(checks.getPatientName(), checks.getIdNumber(), null, null);
        if (patient == null) {
            return AjaxResult.error("患者姓名或身份证号输入错误");
        }
        if (!"11".equals(patient.getUserType())) {
            return AjaxResult.error("该用户不属于患者用户");
        }
        SysUser doctor = userService.checkPatientAndDoctor(null, null, checks.getDoctorName(), checks.getDoctorId());
        if (doctor == null) {
            return AjaxResult.error("医生姓名或工号输入错误");
        }
        if (!"00".equals(doctor.getUserType()) || "admin".equals(doctor.getUserName())) {
            return AjaxResult.error("该用户不属于医生用户");
        }
        return toAjax(checksService.updateChecks(checks));
    }

    /**
     * 删除检查结果
     */
    @PreAuthorize("@ss.hasPermi('medical:checks:remove')")
    @Log(title = "检查结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(checksService.deleteChecksByIds(ids));
    }
}
